perf(mempool): collapse queue Push to single write-lock#1069
Draft
perf(mempool): collapse queue Push to single write-lock#1069
Conversation
Remove the separate isFull() RLock check from Push() and perform the capacity check inside the write lock. This eliminates a TOCTOU window between the capacity check and the actual push, and reduces lock round-trips from two to one per insertion. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1069 +/- ##
==========================================
- Coverage 64.11% 64.10% -0.01%
==========================================
Files 331 331
Lines 23303 23304 +1
==========================================
- Hits 14941 14940 -1
- Misses 6946 6948 +2
Partials 1416 1416
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Push()from a two-lock pattern (RLock forisFull()→ Lock for insert) into a single write-lock with inline capacity checkmaxSizeisFull()helperMotivation
The original
Push()calledisFull()which acquired anRLock, released it, then acquired a fullLockfor the actual insert. Between the two locks, another goroutine could insert, meaning:isFull()check could pass, but by the timeLockis acquired the queue is already full — silently exceedingmaxSizeisFull()could reject when there was actually space — unnecessary rejections under contentionThe fix is a single
Lockthat checks capacity and inserts atomically.Benchmark Results (benchstat, 5 runs, Apple M4 Pro)
This change affects the Insert path, not SelectBy directly. However, the iterator benchmark shows indirect improvement from reduced lock contention between the insert goroutine and the background queue drainer:
Files Changed
mempool/internal/queue/queue.goPush()with single Lock; removeisFull()Test Plan
go test -tags=test -race ./mempool/...— 88 tests pass with race detection🤖 Generated with Claude Code